package com.cqs.leetcode.math;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * lixw
 * 2021/9/5
 **/
public class CompareVersionNumbers {

    public int compareVersion(String version1, String version2) {
        //小数点进行分割
        String[] s1 = version1.split("\\.");
        String[] s2 = version2.split("\\.");
        int i = 0;
        for (i = 0; i < s1.length && i < s2.length; i++) {
            int cmp = Integer.compare(toInt(s1[i]) , toInt(s2[i]));
            if (cmp != 0) return cmp;
        }
        int sign = 1;
        if (i >= s1.length) {
            s1 = s2;
            sign = -1;
        }
        for (; i < s1.length; i++) {
            int val = sign * Integer.compare(toInt(s1[i]) , 0);
            if (val != 0) return val;
        }
        return 0;
    }

    private int toInt(String s) {
        int sum = 0;
        for (int i = 0; i < s.length(); i++) {
            sum = 10 * sum + s.charAt(i) - '0';
        }
        return sum;
    }


    public static void main(String[] args) {
        CompareVersionNumbers c  = new CompareVersionNumbers();
        Map<String,String> cases = new LinkedHashMap<>();
        cases.put("0.1","1.1");
        cases.put("1.05","1.1");
        cases.put("1.06","1.100");
        cases.forEach((k,v)->{
            System.out.println(k+":"+v+"-->" + c.compareVersion(k,v));
        });
    }
}
